package com.olocat.cjx.nsims.controller

import com.alibaba.fastjson.JSON
import com.alibaba.fastjson.JSONObject
import com.olocat.cjx.nsims.dao.EntryDoctorRepository
import com.olocat.cjx.nsims.dao.OperationLogRepository
import com.olocat.cjx.nsims.pojo.entry.EntryDoctor
import com.olocat.cjx.nsims.pojo.info.OperationLog
import com.olocat.cjx.nsims.pojo.personnel.User
import com.olocat.cjx.nsims.service.DoctorService
import com.olocat.cjx.nsims.service.UserService
import com.olocat.cjx.nsims.util.Response
import org.slf4j.Logger
import org.springframework.web.bind.annotation.*
import java.sql.Timestamp
import java.util.*
import javax.annotation.Resource

@RestController
class EntryDoctorController {
    @Resource
    lateinit var doctorService: DoctorService

    @Resource
    lateinit var userService: UserService

    @Resource
    lateinit var entryDoctorRepository: EntryDoctorRepository

    @Resource
    lateinit var operationLogRepository: OperationLogRepository

    @Resource
    lateinit var log: Logger

    @GetMapping("/api/doctor/entry")
    fun getEntry(id: String?): Response {
        return if (id == null) {
            log.info("获取入职医生列表")
            Response.builder(entryDoctorRepository.findAll())
        } else {
            Response.builder(entryDoctorRepository.getOne(id))
        }
    }

//    @PostMapping("/api/doctor/entry")
//    fun add(@RequestBody json: String?): Response {
//        log.info("添加医生信息 json: $json")
//        val doctor = JSON.parseObject(json, EntryDoctor::class.java)
////        nurseService.save(nurse)
//        entryDoctorRepository.save(doctor)
//        return Response.builder()
//    }

    @PostMapping("/api/doctor/entry/agree")
    fun agree(@RequestBody json: JSONObject): Response {
        val doctor = entryDoctorRepository.getOne(json["id"].toString())
        val ol = OperationLog(json["type"].toString(), json["name"].toString(), 0,
                Timestamp(Date().time), json["id"].toString(), "-")
        println(ol)
        when {
            doctor.step > 4 -> {
                return Response.builder()
            }
            doctor.step == 3 -> {
                doctorService.save(doctor.toDoctor())
                val user = User()
                user.telephone = doctor.phone
                user.name = doctor.name
                userService.saveDoctorUser(user)
                doctor.step++
                doctor.state = 1
            }
            else -> {
                doctor.step++
            }
        }
        println(doctor)
        entryDoctorRepository.save(doctor)
        operationLogRepository.save(ol)
        return Response.builder()
    }

    @PostMapping("/api/doctor/entry/reject")
    fun reject(@RequestBody json: JSONObject): Response {
        val doctor = entryDoctorRepository.getOne(json["id"].toString())
        val ol = OperationLog(json["type"].toString(), json["name"].toString(), 1,
                Timestamp(Date().time), json["id"].toString(), "-")
        doctor.state = 2
        entryDoctorRepository.save(doctor)
        operationLogRepository.save(ol)
        return Response.builder()
    }

    @DeleteMapping("/api/doctor/entry/del")
    fun del(@RequestBody json: JSONObject): Response {
        entryDoctorRepository.deleteById(json["id"].toString())
        return Response.builder()
    }
}